!pr1
!lm12
!rm75
Revisiting $48:0...........................Bob Sander-Cederlof

Remember all those warnings about storing 0 in $48 after DOS had a whack at your zero page?  Maybe not, but let me remind you.  

Apple's monitor uses locations $45 through $49 in a very special way.  Ignoring this, the writers of DOS also used them.  When you start execution from the monitor (using the G, S, or T commands) The data in these locations gets loaded into the registers:  $45 into A, $46 into X, $47 into Y, $48 into P (status), and $49 into S (stack pointer).  When a program hits a BRK opcode, or the S command has finished executing a single opcode, the monitor saves these five registers back into $45...$49.

No serious problem, unless you like to enter the monitor and issue the G, S, or T commands.  Even less of a problem, because the S and T commands were removed from the monitor ROM when the Apple II Plus came out.  And if you don't care what is in the registers anyway....

But the P-register is rather special, too.  One of its bits, called "D", controls how arithmetic is performed.  If "D" is zero, arithmetic will be done in the normal binary way; if D=1, arithmetic is done in BCD mode.  That is, adding one to $49 will produce $50 rather than $4A.  If the program you are entering doesn't expect to be in decimal mode, and tries arithmetic, you will get some rather amusing results.

Hence the warning:  before using the G command from the monitor, type 48:0 to be sure decimal mode is off.  Later versions of DOS store 0 into $48 after calling those routines which use $48.  And the monitor stores 0 into $48 whenever you hit the RESET key (or Control-RESET).

******************************************************
*                                                    *
*   Now I am here to tell you that storing 0 into    *
*   $48 is ALL WRONG!  It took Bill and me 5 hours   *
*   to unravel the mystery caused by storing zero    *
*   there!                                           *
*                                                    *
******************************************************

You should put into $48 a sensible value.  Better, DOS should never use $45 through $48; if it must use them, save and restore them.  There are eight bits in the P-register, and in the 6502 seven of them are important.  One of them, we discovered, is VERY important.

The bit named "I" controls the IRQ interrupt.  If I=1, IRQ interrupts will not be accepted.  If I=0, IRQ interrupts will be accepted.  So...who cares about interrupts?

Hardly anyone uses interrupts in Apple II's, because of all the hidden problems.  But there are some very nice boards for the Apple that are designed to be used with interrupts.  Most of them are safe, because RESET disables their interrupt generators.

Need I say that we discovered a board that does not disable the interrupt generators when you hit RESET?  The Novation Cat Modem (a very excellent product) leaves at least one of its potential IRQ sources in an indeterminate state.  IRQ's don't immediately show up, though, because they are trapped until you have addressed any of the soft switches on the card.  But, for example, if that card is in slot 2 and I read or write any location from $C0A0 through $C0AF, IRQ's start coming.  Still no problem, because I=1 in the P-register.

UNTIL WE USE THE MONITOR G COMMAND!

If I use the monitor G command, location $48, containing 0, is loaded into the P-register.  Then an IRQ gets through and sends the 6502 vectoring through an unprepared vector at $3FE,3FF and BANG!

Our solution was to put SEI instructions in various routines, and to make sure that $48 contains 4, not 0, before using the G command.

From now on, whenever you hear that you need to be sure $48 contains zero, think four.
